import classNames from "classnames";
import "./index.scss";
import { useMemo, useState } from "react";
import Icon from "./Icon";
import dayjs from "dayjs";
import { billTypeToName } from "@/contants";


const DailyBill = ({ date, billList }) => {
  const result = useMemo(() => {
    const pay = billList
      .filter((item) => item.type === "pay")
      .reduce((a, c) => a + c.money, 0);
    const income = billList
      .filter((item) => item.type === "income")
      .reduce((a, c) => a + c.money, 0);
    return {
      pay,
      income,
      total: pay + income,
    };
  }, [billList]);
  
  const [visible, setvisible] = useState(false)
  return (
    <div className={classNames("dailyBill")}>
      <div className="header">
        <div className="dateIcon">
          <span className="date">{dayjs(date).format("MM-DD")}</span>
          <span className={classNames("arrow")} onClick={() => setvisible(!visible)}></span>
        </div>
        <div className="oneLineOverview">
          <div className="pay">
            <span className="type">支出</span>
            <span className="money">{result.pay}</span>
          </div>
          <div className="income">
            <span className="type">收入</span>
            <span className="money">{result.income}</span>
          </div>
          <div className="balance">
            <span className="money">{result.total}</span>
            <span className="type">结余</span>
          </div>
        </div>
      </div>
      {/* 单日列表 */}
      <div className="billList" style={{ display: visible ? "block" : "none" }}>
        {billList.map((item) => {
          return (
            <div className="bill" key={item.id}>
               
              <Icon type={item.useFor} />
              <div className="detail">
                <div className="billType"> {billTypeToName[item.useFor]}</div>
              </div>
              <span>{item.money}</span>
            </div>
          );
        })}
      </div>
    </div>
  );
};
export default DailyBill;
